package com.alstudio.utils.activity;

import java.util.List;
import java.util.Stack;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.text.TextUtils;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.event.ALEventDeliver;
import com.alstudio.ui.base.BaseActivity;

public class ALActivityManager {

	private static ALActivityManager instance;

	private static Stack<BaseActivity> activityStack;
	/* 界面管理器 */
	private ActivityManager activityManager;

	private String packageName;

	private ActivityLifeCycleListener mListener;
	// current showed activity
	private BaseActivity curActivity;

	public ALActivityManager() {
		// TODO Auto-generated constructor stub
		packageName = ALLocalEnv.getLocalEnv().getPackageName();
		activityManager = (ActivityManager) ALLocalEnv.getLocalEnv()
				.getSystemService(Context.ACTIVITY_SERVICE);

	}

	public static ALActivityManager getALActivityManager() {
		if (instance == null) {
			instance = new ALActivityManager();
		}
		return instance;
	}

	public BaseActivity getCurrentActivity() {
		return curActivity;
	}

	public ActivityLifeCycleState getCurrentActivityState() {
		return curActivity.getActivityState();
	}

	public boolean isActivityOnTop(BaseActivity activity) {
		boolean onTop = false;

		if (activity == null) {
			throw new IllegalArgumentException("your activity is null!");
		}

		if (curActivity.equals(activity)) {
			onTop = true;
		}
		return onTop;
	}

	public boolean isActivityOnTop(String tag) {
		boolean onTop = false;

		try {
			if (TextUtils.isEmpty(tag)) {
				throw new IllegalArgumentException("your activity is null!");
			}

			if (curActivity.activityName.equals(tag)) {
				onTop = true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return onTop;
	}

	/**
	 * 
	 * @param activity
	 */
	public void addActivity(BaseActivity activity) {
		if (activityStack == null) {
			activityStack = new Stack<BaseActivity>();
		}
		activityStack.add(activity);
	}

	/**
	 * 
	 * @param activity
	 */
	public void deleteActivity(BaseActivity activity) {
		if (activity != null) {
			activity.finish();
			activityStack.remove(activity);
			activity = null;
		}
	}

	/**
	 * 判断程序是否在前台运行。
	 * 
	 * @return true 正在前台运行；false 已经退到后台运行
	 * @author Alonso Lee
	 */
	public boolean isAppOnForeground() {
		List<RunningTaskInfo> rts = null;
		try {
			rts = activityManager.getRunningTasks(1);
		} catch (SecurityException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if (rts == null) {
			return false;
		}
		if (rts != null && rts.size() > 0) {

			try {
				if (packageName
						.equals(rts.get(0).baseActivity.getPackageName())) {
					return true;
				}
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		}
		return false;
	}

	public void setActivityLifeCycleListener(ActivityLifeCycleListener listener) {
		mListener = listener;
	}

	public ActivityLifeCycleListener getActivityLifeCycleListener() {
		return mListener;
	}

	public void notifyLifeCycleStateChanged(ActivityLifeCycleState state,
			BaseActivity actvity) {
		if (state == ActivityLifeCycleState.RESUME) {
			curActivity = actvity;
		} else if (state == ActivityLifeCycleState.PAUSE) {
			// 检查程序是否还在后台运行
			ALEventDeliver.getDefaultAlEventDeliver().postALEvent(
					isAppOnForeground());
		}

		if (mListener != null) {
			switch (state) {
			case CREATE:
				mListener.onActivityCreate(actvity);
				break;
			case START:
				mListener.onActivityStart(actvity);
				break;
			case RESUME:
				mListener.onActivityResume(actvity);
				// set current activity
				break;
			case PAUSE:
				mListener.onActivityPause(actvity);
				break;
			case STOP:
				mListener.onActivityStop(actvity);
				break;
			case FINISH:
				mListener.onActivityFinish(actvity);
				break;
			case DESTROY:
				mListener.onActivityDestroy(actvity);
				break;
			}
		}
	}

	public interface ActivityLifeCycleListener {
		public void onActivityCreate(BaseActivity actvity);

		public void onActivityStart(BaseActivity actvity);

		public void onActivityResume(BaseActivity actvity);

		public void onActivityPause(BaseActivity actvity);

		public void onActivityStop(BaseActivity actvity);

		public void onActivityFinish(BaseActivity actvity);

		public void onActivityDestroy(BaseActivity actvity);
	}

	public enum ActivityLifeCycleState {
		CREATE, START, RESUME, PAUSE, STOP, FINISH, DESTROY,
	}
}
